import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SearchTask {
	/* TODO:
	 * Need to refactor the class
	 */
	/**
	 * This method is called to execute the search command.
	 * @param tasksInTempStorage
	 * @param tasksOnDisplay
	 * @param typeTasksOnDisplay
	 */
	public void execute(List<Task> tasksInTempStorage,
			List<Task> tasksOnDisplay, String typeTasksOnDisplay) {
		List<Task> tasksContainingSearch = new ArrayList<Task>();
		String searchParameter = typeTasksOnDisplay;
		String message;
		
		if (searchParameter.equals("")) {
			message = "no parameter provided for search";
			TaskO.taskOMainScreen.appendOutput(message);
			//TODO: Clean up this method
			System.out.println(message);
		} 
		else {
			this.clearCurrentTasksOnDisplay(tasksOnDisplay);
			tasksContainingSearch = this.searchFromTempStorage(tasksInTempStorage,
					typeTasksOnDisplay);
			this.sortSearchListOnPriority(tasksContainingSearch, tasksOnDisplay);
		}
	}

	// TODO 1 - what do I put here
	private void clearCurrentTasksOnDisplay(List<Task> tasksOnDisplay) {
		try {
			tasksOnDisplay.clear();
		} catch (Exception e) {
			System.out.println("the tasks on display may be shown incorrectly");
		}
	}
	
	private List<Task> searchFromTempStorage(List<Task> tasksInTempStorage,
			String typeTasksOnDisplay) {
		List<Task> searchList = new ArrayList<Task>();
		int searchPriority = -1;
		String searchParameter = typeTasksOnDisplay.toLowerCase();
		boolean isFoundInSearch;
		
		for(int i=0; i<tasksInTempStorage.size(); i++) {
			isFoundInSearch = true;
			Task task = tasksInTempStorage.get(i);
			
			if(task.getTaskTitle().toLowerCase().contains(typeTasksOnDisplay)) {
				searchPriority=2;
				task.setTaskSearchPriority(searchPriority);
			}
			else if(task.getTaskStartDate().toLowerCase().contains(typeTasksOnDisplay)) {
				searchPriority=3;
				task.setTaskSearchPriority(searchPriority);
			}
			else if(task.getTaskEndDate().toLowerCase().contains(typeTasksOnDisplay)) {
				searchPriority=3;
				task.setTaskSearchPriority(searchPriority);
			}
			else if(task.getTaskDescription().toLowerCase().contains(typeTasksOnDisplay)) {
				searchPriority=4;
				task.setTaskSearchPriority(searchPriority);
			}
			else isFoundInSearch = false;
		
			if(task.getIsTaskDone() && isFoundInSearch==true) {
				searchPriority=5;
				task.setTaskSearchPriority(searchPriority);
			}

			if(task.getIsTaskStarred() && isFoundInSearch==true) {
				searchPriority=searchPriority-1;
				task.setTaskSearchPriority(searchPriority);
			}
			
			if(isFoundInSearch == true) {
			searchList.add(task);
			}
			
		}
		return searchList;
	}
	
	/**
	 * Searches the vector according to priorities chosen by the user. Clusters the results
	 * according to various combinations. E.g. starred only, (or) starred + timed task, (or) 
	 * starred + deadlines etc.
	 * @param tasksContainingSearch
	 * @param tasksOnDisplay
	 */
	private void sortSearchListOnPriority(
			List<Task> tasksContainingSearch, List<Task> tasksOnDisplay) {
	
		Task[] arrayForTasksStorage= new Task[tasksContainingSearch.size()];
		for(int i = 0; i<tasksContainingSearch.size(); i++) {
			arrayForTasksStorage[i] = tasksContainingSearch.get(i);
		}
		Arrays.sort(arrayForTasksStorage);
		
		for(int i = 0; i<arrayForTasksStorage.length; i++) {
			tasksOnDisplay.add(arrayForTasksStorage[i]);
		}
		
	}
	
 }
